// pages/discover/news/news-detail.js
const app = getApp()
import utils from '../../utils/util.js'
import Dialog from '@vant/weapp/dialog/dialog'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    forumId: '',
    blog: {},
    comments: [],
    commentContent: '',
    userInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    let bid = options.bid
    this.setData({forumId: bid, userInfo: app.globalData.userInfo})
    this.loadBlogDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  loadBlogDetail: function(){
    let that = this
    utils.request(
      app.globalData.apiBaseUrl+'/forum/detail?forumId='+this.data.forumId,
      {}
    ).then(res=>{
      wx.stopPullDownRefresh()
      that.setData({
        blog: res.data,
      })
    })
  },

  //浏览大图
  showLargeImage(e) {
    let currIdx = e.currentTarget.dataset.idx //被点击的图片在该笔记中的下标，比如有五张图片，被点的是第3张，currIdx则为2
    let curr = ''
    let imgList = []
    for(var idx=0; idx<this.data.blog.picList.length; idx++){
      imgList.push(this.data.blog.picList[idx])
      if(currIdx==idx){
          curr = this.data.blog.picList[idx]
      }
    }
    wx.previewImage({
        urls: imgList, //需要预览的图片http链接列表，注意是数组
        current: curr, // 当前显示图片的http链接，默认是第一个
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
  },

  onInputContent: function(e){
    this.setData({ commentContent: e.detail.value })
  },

  //评论
  publishComment: function(e){
    let that = this
    let req = {forumId: this.data.forumId, content: this.data.commentContent}
    utils.request(
      app.globalData.apiBaseUrl+'/forum/comment/save',
      req,
      'POST'
    ).then(res=>{
      that.loadBlogDetail()
      wx.showToast({
        icon: 'success',
        title: '评论成功',
      })
      that.setData({commentContent: ''})
    })
  },

})